package com.tb;

import com.google.common.base.Joiner;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.message.BasicNameValuePair;
import org.junit.Test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 助通信息科技有限公司 短信api
 *
 * @since 2017/11/13.
 */
public class SMSTest {

    @Test
    public void testSendSMS() throws IOException {
        String url = "http://api.zthysms.com/sendSms.do";
        String ts = formatDate();
        Response execute = Request.Post(url).bodyForm(new NameValuePair[]{
                new BasicNameValuePair("username", "iuhoyx"),
                new BasicNameValuePair("tkey", ts),
                new BasicNameValuePair("password", buildPasswordCipher("8Ip07E", ts)),
                new BasicNameValuePair("mobile", "13980694173"),
                new BasicNameValuePair("content", URLEncoder.encode("【九月女装专卖店】别让优惠券发霉了，新品9折会籍折上折，全部支持优惠劵哦。", "utf-8"))
        }).execute();



        String s = execute.handleResponse(new BasicResponseHandler());
        System.out.println(s);
//
//
//        Map<String, Object> params = new HashMap<>();
//        params.put("username", "iuhoyx");
//        params.put("tkey", ts);
//        params.put("password", buildPasswordCipher("8Ip07E", ts));
//        params.put("mobile", "13980694173");
//        params.put("content", URLEncoder.encode("【九月女装专卖店】别让优惠券发霉了，新品9折会籍折上折，全部支持优惠劵哦。", "utf-8"));
//
//        String parameter = Joiner.on("&").withKeyValueSeparator("=").join(params);
//        Response execute2 = Request.Post("http://api.zthysms.com/sendSms.do?" + parameter).execute();
//        String s2 = execute2.handleResponse(new BasicResponseHandler());
//        System.out.println(s2);
    }


    @Test
    public void testSMSBatch() throws IOException {
        String url = "http://api.zthysms.com/sendSmsBatch.do";
        String ts = formatDate();
//        Response execute = Request.Post(url).bodyForm(new NameValuePair[]{
//                new BasicNameValuePair("username", "iuhoyx"),
//                new BasicNameValuePair("tkey", ts),
//                new BasicNameValuePair("password", buildPasswordCipher("8Ip07E", ts)),
//                new BasicNameValuePair("mobile", "15882169011"),
//                new BasicNameValuePair("content", URLEncoder.encode("【九月女装专卖店】别让优惠券发霉了，新品9折会籍折上折，全部支持优惠劵哦。", "utf-8"))
//        }).execute();
//
//        String s = execute.handleResponse(new BasicResponseHandler());
//        System.out.println(s);




        Map<String, Object> params = new HashMap<>();
        params.put("username", "iuhoyx");
        params.put("tkey", ts);
        params.put("password", buildPasswordCipher("8Ip07E", ts));
        params.put("mobile", "13980694173,15882169011");
        params.put("content", URLEncoder.encode("【九月女装专卖店】别让优惠券发霉了，新品9折会籍折上折，全部支持优惠劵哦。", "utf-8"));
        String parameter = Joiner.on("&").withKeyValueSeparator("=").join(params);
        Response execute2 = Request.Post(url + "?" + parameter).execute();
        String s2 = execute2.handleResponse(new BasicResponseHandler());
        System.out.println(s2);
    }


    @Test
    public void testSMSNoticeByForm() throws IOException {
        String url = "http://api.zthysms.com/sendSmsBatch.do";
        String ts = formatDate();
        String content = "【九月女装店】您购买商品收到了吧，我们有好评返现活动哦 ！五星好评返现3元，晒图五星好评返现7元！";

        Response execute = Request.Post(url).bodyForm(new NameValuePair[]{
                new BasicNameValuePair("username", "iuhohy"),
                new BasicNameValuePair("tkey", ts),
                new BasicNameValuePair("password", buildPasswordCipher("kgIFOf", ts)),
                new BasicNameValuePair("mobile", "13980694173"),
                new BasicNameValuePair("content", URLEncoder.encode(content, "utf-8"))
        }).execute();
        String s2 = execute.handleResponse(new BasicResponseHandler());
        System.out.println(s2);

    }


    @Test
    public void testSMSNotice() throws IOException {
        String url = "http://api.zthysms.com/sendSmsBatch.do";
        String ts = formatDate();
        String content = "【九月女装店】您购买商品收到了吧，我们有好评返现活动哦 ！五星好评返现3元，晒图五星好评返现7元！";

        Map<String, Object> params = new HashMap<>();
        params.put("username", "iuhohy");
        params.put("tkey", ts);
        params.put("password", buildPasswordCipher("kgIFOf", ts));
        params.put("mobile", "13980694173");
        params.put("content", URLEncoder.encode(content, "utf-8"));

        String parameter = Joiner.on("&").withKeyValueSeparator("=").join(params);
        Response execute2 = Request.Post(url + "?" + parameter).execute();
        String s2 = execute2.handleResponse(new BasicResponseHandler());
        System.out.println(s2);

    }

    private String buildPasswordCipher(String password, String tsStr) {
        return MD5Util.MD5(MD5Util.MD5(password) + tsStr);
    }

    private String formatDate() {
        return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    }

    @Test
    public void testApacheDigestUtils(){
        String content = "调用方式：\n" +
                "助通短信API是使用HTTP协议的 Web服务接口，您可以使用几乎任何客户端和任何编程语言与助通API进行交互。通过发送简单的HTTP POST请求就可以轻松接入使用。\n" +
                "注：为确保您的账户和信息安全，请在功能上线前，联系助通客服，添加您要使用的IP地址到 IP白名单列表中。\n" +
                "统一请求格式：\n" +
                "URL：\n" +
                "http://api.zthysms.com/{ function }.do\n" +
                "说明：{function}为该资源提供的操作方法，接口返回值为string字符串。\n" +
                "比如查账户余额信息的url为：http://api.zthysms.com/balance.do表示调用余额查询方法，我们目前已经提供的接口，请参考API。\n" +
                "HTTP头信息:\n" +
                "Content-Type:application/x-www-form-urlencoded;charset=utf-8;\n" +
                "Accept:application/json;charset=utf-8;\n" +
                "说明：请求方式(Method)：统一用POST方式，编码：UTF-8。";
        String mine = MD5Util.MD5(content);
        String apache = DigestUtils.md5Hex(content);
        assert apache.equals(mine);
    }
}
